package com.jy.controller;


import com.aliyuncs.IAcsClient;
import com.jy.common.config.ALiYunOSSConfig;
import com.jy.common.enums.ResponseWrapper;
import com.jy.common.model.user.param.DeleteFileByOssUrl;
import com.jy.entity.Alioss;
import com.jy.service.AliossService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 阿里云配置信息表 前端控制器
 * </p>
 *
 * @author fzg
 * @since 2024-11-22
 */
@RestController
@RequestMapping("/alioss")
public class AliossController {

    private final AliossService aliossService;

    public AliossController(AliossService aliossService, ALiYunOSSConfig ossConfig, IAcsClient client) {
        this.aliossService = aliossService;
        this.ossConfig = ossConfig;
        this.client = client;
    }

    /**
     * 获取阿里云对象存储oss配置信息
     */
    @GetMapping("/getAliossConfig")
    public HashMap<String,Object> getAliossConfig(){
        Alioss config = aliossService.getById(1);
        HashMap<String,Object> map = new HashMap<>();
        map.put("config",config);
        return map;
    }

    /**
     * 获取阿里云oss临时凭证
     */
    private final ALiYunOSSConfig ossConfig;

    private final IAcsClient client;
    @GetMapping("/getOssStsToken")
    public Map<String, Object> getOssStsToken() {
        return ossConfig.getOSSStsToken(client);
    }

    /**
     * 获取阿里云短信验证码
     * @param phone
     * @return
     */
    @GetMapping("/getCode")
    public ResponseWrapper getCode(@Validated String phone) {
        return aliossService.getCode(phone);
    }


    /**
     * 根据阿里云文件的URL地址删除该文件
     * @param param URL地址和token
     * @return ResponseWrapper
     */
    @PostMapping("/deleteFileByOssUrl")
    public ResponseWrapper deleteFileByOssUrl(@RequestBody @Validated DeleteFileByOssUrl param){
        return aliossService.deleteFileByOssUrl(param);
    }

}
